/*
Launchy: Application Launcher
Copyright (C) 2007  Josh Karlin

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


#ifdef Q_WS_WIN
#include <windows.h>
#include <shlobj.h>
#include <tchar.h>

#endif

#include "gspell.h"
#include <QDebug>
#include <QUrl>
#include <QClipboard>
#include <QApplication>

gspellPlugin* ggspellInstance = NULL;

void Process::run()
{
	found = false;
	QString webQuery = "/search?source=launchy&q=";
//	query.replace("?","");	
	query = QUrl::toPercentEncoding(query);
	webQuery += query;
	//qDebug() << webQuery;
	resBuffer.open(QIODevice::ReadWrite);

    connect(&http, SIGNAL(done(bool)), this, SLOT(httpGetFinished(bool)));
	http.setHost("www.google.com");	
	http.get(webQuery, &resBuffer);	

	loop.exec();
} 

void Process::httpGetFinished(bool error) {

	if (!error) {

		result = resBuffer.data();
		QRegExp regex_res(";spell=1\" class=p><b>([^&]*)", Qt::CaseInsensitive);
		//qDebug() << "\n\n" + result;
		if (regex_res.indexIn(result) != -1) {
			found = true;
		    result =regex_res.cap(1).remove("<i>").remove("</i>").remove("<b>").remove("</b>").remove("</a>").append("*");
			//qDebug() << "\n\n" + result;
		
		} else {
			result = query.replace("%20"," ");
		}

	} else {
		result = "Error";
	}
	loop.exit();
}


void gspellPlugin::getID(uint* id)
{
	*id = HASH_gspell;
}

void gspellPlugin::getName(QString* str)
{
	*str = PLUGIN_NAME;
}

void gspellPlugin::init()
{
}



void gspellPlugin::getLabels(QList<InputData>* id)
{
}

void gspellPlugin::getResults(QList<InputData>* id, QList<CatItem>* results)
{
	if (id->count() != 2)
		return;

	const QString & text = id->first().getText();

	if (!text.contains("gspell", Qt::CaseInsensitive))
		return;

	const QString & query = id->last().getText();

//	if (!query.contains("?", Qt::CaseInsensitive))
//		return;

	Process p;
	p.query = query;
	p.run();

//	results->clear();
	if (results->size() == 0){
		found = p.found;
		result = p.result;
		results->push_front(CatItem(p.result + ".gspell", p.result, HASH_gspell, getIcon()));
	}
}

void gspellPlugin::setPath(QString * path) {
    libPath = *path;

}

QString gspellPlugin::getIcon()
{

	
	if(found){
		return libPath + "/icons/gspell_no.png";
	}
	else
	{
		return libPath + "/icons/gspell_yes.png";
	}


}

void gspellPlugin::getCatalog(QList<CatItem>* items)
{
	items->push_back(CatItem("GSpell.gspell", "GSpell", HASH_gspell, getIcon()));

}

void gspellPlugin::launchItem(QList<InputData>* id, CatItem* item)
{
	qDebug() << result;
	QClipboard *clipboard = QApplication::clipboard();
	clipboard->setText(result.remove("*"));
}

void gspellPlugin::doDialog(QWidget* parent, QWidget** newDlg) 
{
}

void gspellPlugin::endDialog(bool accept) 
{
}
int gspellPlugin::msg(int msgId, void* wParam, void* lParam)
{
	bool handled = false;
	switch (msgId)
	{		
		case MSG_INIT:
			init();
			handled = true;
			break;
		case MSG_GET_LABELS:
			getLabels((QList<InputData>*) wParam);
			handled = true;
			break;
		case MSG_GET_ID:
			getID((uint*) wParam);
			handled = true;
			break;
		case MSG_GET_NAME:
			getName((QString*) wParam);
			handled = true;
			break;
		case MSG_GET_RESULTS:
			getResults((QList<InputData>*) wParam, (QList<CatItem>*) lParam);
			handled = true;
			break;
		case MSG_GET_CATALOG:
			getCatalog((QList<CatItem>*) wParam);
			handled = true;
			break;
		case MSG_LAUNCH_ITEM:
			launchItem((QList<InputData>*) wParam, (CatItem*) lParam);
			handled = true;
			break;
				 

		case MSG_HAS_DIALOG:
			// Set to true if you provide a gui
			handled = false;
			break;
		case MSG_DO_DIALOG:
			// This isn't called unless you return true to MSG_HAS_DIALOG
			doDialog((QWidget*) wParam, (QWidget**) lParam);
			break;
		case MSG_END_DIALOG:
			// This isn't called unless you return true to MSG_HAS_DIALOG
			endDialog((bool) wParam);
			break;
		case MSG_PATH:
			setPath((QString *) wParam);	    
		default:
			break;

	}
		
	return handled;
}

Q_EXPORT_PLUGIN2(gspell, gspellPlugin) 
